﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EHR.Common;
using CommunitySystem.DiaoDongService;
using System.Collections.ObjectModel;
using System.Windows.Data;
using CommunitySystem.Assert;
using CommunitySystem.Ctrls;
using CommunitySystem.Views;
using CommunitySystem.PageView;

namespace CommunitySystem
{
    public partial class ZJZZRListControl : AsyncCtrl, IReturnListenerc
    {
        DiaoDongServiceClient client = new DiaoDongServiceClient();
        public NavMoveSwitch Nav { get; set; }
        public ZJZZRListControl()
        {
            InitializeComponent();

            IsWorking.Visibility = Visibility.Visible;
            busy.BusyContent = "正在同步数据，请稍后...";

            //模块发起由管理员进行操作 新增 添加整建制转出单位 和 整建制转入单位
            if (Session.LoginUser.RoleID != 1) 
			{
                this.btnNew.Visibility = Visibility.Collapsed;
				  this.btnAddEmp.Visibility = Visibility.Collapsed;
				
			}
            else
			{
                this.btnNew.Visibility = Visibility.Visible;
                this.btnAddEmp.Visibility = Visibility.Visible;
			}
            client.GetZJZZRModelCountCompleted += new EventHandler<GetZJZZRModelCountCompletedEventArgs>(client_GetZJZZRModelCountCompleted);

            client.GetZJZZRModelCompleted += new EventHandler<GetZJZZRModelCompletedEventArgs>(client_GetZJZZRModelCompleted);

            Load();
        }

        void client_GetZJZZRModelCompleted(object sender, GetZJZZRModelCompletedEventArgs e)
        {
            dg.ItemsSource = null;
            dg.ItemsSource = e.Result;
            IsWorking.Visibility = Visibility.Collapsed;
        }
        private int pageSize = 20;
        void client_GetZJZZRModelCountCompleted(object sender, GetZJZZRModelCountCompletedEventArgs e)
        {
            ObservableCollection<int> list = new ObservableCollection<int>();
            int last = e.Result % pageSize;
            int count = 0;
            if (last > 0)
                count = e.Result / pageSize + 1;
            else
                count = e.Result / pageSize;
            for (int i = 0; i < count; i++)
            {
                list.Add(i);
            }

            PagedCollectionView pcv = new PagedCollectionView(list);
            dataPager1.PageSize = 1;
            dataPager1.Source = null;
            dataPager1.Source = pcv;
        }
        void Load()
        {
         
            string tj = string.Empty;
            if (Session.LoginUser.RoleID != 1)
                tj = Session.LoginUser.LoginName;
            client.GetZJZZRModelCountAsync(tj);
        }
        /// <summary>
        /// 人员转任
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnZR_Click(object sender, RoutedEventArgs e)
        {
            if (this.dg.SelectedItem == null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择一条整建制转入信息!");
                return;
            }
            var sel = this.dg.SelectedItem as ZJZZRModel;
            ZREMPControl view = new ZREMPControl(sel);
            Nav.ShowNextPage(sel.ZCDW + "-单位人员转任", view, this);
        }
        /// <summary>
        /// 添加整建制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnNew_Click(object sender, RoutedEventArgs e)
        {

            ZJZZRWindow win = new ZJZZRWindow();
            win.Show();
            win.Closed += new EventHandler(win_Closed);
        }
        /// <summary>
        /// 添加人员
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnAddEmp_Click(object sender, RoutedEventArgs e)
        {
            if (this.dg.SelectedItem == null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择一条整建制转入信息!");
                return;
            }
            var sel = this.dg.SelectedItem as ZJZZRModel;
            if (sel.BZ == "人员已转任")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("整建制转入人员已全部转入，不可再操作！");
                return;
            }
            AddZCEmpWindow win = new AddZCEmpWindow(sel);
            win.Show();
            win.Closed += new EventHandler(win_Closed);
        }
        void win_Closed(object sender, EventArgs e)
        {
            Load();
        }

        private void dataPager1_PageIndexChanged(object sender, EventArgs e)
        {
            int index = dataPager1.PageIndex == -1 ? 0 : dataPager1.PageIndex;
            string tj = string.Empty;
            if (Session.LoginUser.RoleID != 1)
                tj = Session.LoginUser.LoginName;
            client.GetZJZZRModelAsync(tj, index + 1, pageSize);
        }

        #region IReturnListenerc 成员

        public void BeginReturn()
        {
           
        }

        #endregion
    }
}
